package tjs.study.notes.dotest.proxy.spring.springLoad;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tjs.study.notes.dotest.TjsStudyNotesApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Nicky.Tang on 2021/9/28 5:06 下午
 *
 * @since 02.12.10
 */

public class DoTestOfSpringLoad extends TjsStudyNotesApplication {
    //将字节数组转成文件
    public static void writeBytes(String className, byte[] bytes) throws IOException {
        File file = new File("/Users/piangpiang/Documents/my-workspace/my-mayun/tjs-study-notes-test2/tjs-study-notes/tjs-study-notes/tjs-study-dotest/src/main/java/tjs/study/notes/dotest/proxy/spring/springLoad/" + className + ".class");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bytes);
        fos.close();
    }

    /**
     * 1. 启动时加上以下参数
     * -javaagent:/Users/piangpiang/Documents/my-workspace/my-mayun/tjs-study-notes-test2/tjs-study-notes/tjs-study-notes/tjs-study-dotest/src/main/java/tjs/study/notes/dotest/proxy/spring/springLoad/springloaded-1.2.8.RELEASE.jar
     * <p>
     * <p>
     * 2. 更改代码后，手动重新编译下
     * <p>
     * todo springloader的SpringLoadedAgent做了什么
     * {@link org.springsource.loaded.agent.SpringLoadedAgent}
     * {@link org.springsource.loaded.agent.ClassPreProcessorAgentAdapter}
     *      // 先获取最新class的实例 todo 如何实现的
     *      UserTestService2 var3 = TypeRegistry.instanceFieldInterceptionRequired(300, "userTestService2222") ? (UserTestService2)this.r$get(this, "userTestService2222") : this.userTestService2222;
     *      // 然后通过java反射调用目标方法，fixme 因此可以调用新增的方法
     *      return TypeRegistry.ivicheck(301, "doTest3()Ljava/lang/String;") ? (String)var3.__execute((Object[])null, var3, "doTest3()Ljava/lang/String;") : var3.doTest3();
     *
     *
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(DoTestOfSpringLoad.class, args);
    }
}
